﻿#include "GateWay.h"
#include <QCoreApplication>
#include <QtMsgHandler>

void outputMessage(QtMsgType type, const char *msg)
{
    static QMutex mutex;
    mutex.lock();
    Q_UNUSED(type);
    QString text;
    QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
    text = currentTime + QString(msg) + "\r\n";
    QByteArray textArray = text.toLatin1();
    fprintf(stderr, textArray.constData());

    QFile file(QCoreApplication::applicationDirPath() + "/log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream textStream(&file);
    textStream << text;
    file.flush();
    file.close();

    mutex.unlock();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

  //  qInstallMsgHandler(&outputMessage);
//    System *system = System::getInstance();
//    QString appDirPath = system->appDirPath();
//     QString rootPath = system->rootPath();
//    BasicXml *basicXml = BasicXml::getInstance();
//    basicXml->setPath(appDirPath + "/Xml/Basic.xml");
//    basicXml->parser();

//    QMap<QString, QMetaObject> portMetaMap;
//    portMetaMap.insert(UART::staticMetaObject.className(), UART::staticMetaObject);

//    QList<PortStruct> portParamList = basicXml->portList();
//    QMap<QString,BasePort*> portMap;
//    foreach (const PortStruct &portParam, portParamList)
//    {
//        if(!portMetaMap.contains(portParam.type()))
//        {
//            qDebug()<<"Error:"+portParam.type()+" is an illegal port type!";
//            continue;
//        }
//        QMetaObject meta = portMetaMap.value(portParam.type());
//        BasePort *port = qobject_cast<BasePort*>(meta.newInstance());
//        if(port == NULL)
//        {
//            continue;
//        }
//        portMap.insert(portParam.name(),port);
//        port->setPortParam(portParam);
//        port->open();
//        PortStruct targeter=port->portParam();
//        QByteArray data1="setChannel 0x12\r\n";
//        QByteArray data2="setTxPower 3\r\n";
//        QByteArray data3="tx 0\r\n";
//        port->write(targeter,data1);
//        QElapsedTimer delayTimer;
//        delayTimer.start();
//        while(delayTimer.elapsed()<=500);
//        port->write(targeter,data2);
//        QElapsedTimer delayTimer1;
//        delayTimer1.start();
//        while(delayTimer1.elapsed()<=500);
//        port->write(targeter,data3);
//    }

    /**************************/

    static QMutex mutex;
    mutex.lock();
    QString text;
    QString currentTime = QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]");
    text = currentTime + "GateWay reboot now" + "\r\n";
    QByteArray textArray = text.toLatin1();
    fprintf(stderr, textArray.constData());

    QFile file(QCoreApplication::applicationDirPath() + "/reboot.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream textStream(&file);
    textStream << text;
    file.flush();
    if(file.size()>0x100000)
    {
        file.remove();
    }
    file.close();

    mutex.unlock();

    /**************************/

    qDebug()<<"runing now----------------------------";
    GateWay gateWay;
    return a.exec();
}
